package com.smartmedical.doctor.medic.service.ipml;

import com.smartmedical.doctor.medic.domain.dto.param.DoctorUserParam;
import com.smartmedical.doctor.medic.domain.vo.DoctorJobTitleVo;
import com.smartmedical.doctor.medic.mapper.MedicMapper;
import com.smartmedical.doctor.medic.service.MedicService;
import com.smartmedical.common.core.domain.HealthR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class MedicServicempl implements MedicService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    MedicMapper medicMapper;

    @Override
    public HealthR applyJoin(DoctorUserParam doctorUserParam) {

        String pwd1 = doctorUserParam.getPwd1();
        String pwd2 = doctorUserParam.getPwd2();
        if (!pwd1.equals(pwd2)) {
            return HealthR.fail("两次密码不一致");
        }
        String s = redisTemplate.opsForValue().get(doctorUserParam.getEmail());
        if (!s.equals(doctorUserParam.getCode())) {
            return HealthR.fail("验证码错误");
        }
        int jobTitleId = doctorUserParam.getJobTitleId();
        String jobTitleName = medicMapper.getJobTitleName(jobTitleId);
        doctorUserParam.setJobTitle(jobTitleName);

        Integer ret = medicMapper.applyJoin(doctorUserParam);
        if (ret > 0) {
            return HealthR.ok("申请成功");
        }
        return HealthR.fail("申请失败");
    }

    @Override
    public HealthR sendEmailCode(String email) {
        String code = "";
        //生成4位随机验证码
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int digit = random.nextInt(10); // 生成0到9的随机数字
            code = code.concat(String.valueOf(digit));
        }
        //查询邮箱是否存在
        String ret = medicMapper.checkEmailUnique(email);
        if (ret == null) {
            return HealthR.fail("邮箱不存在");
        }

        redisTemplate.opsForValue().set(email,code);

        return HealthR.ok("验证码发送成功："+code,"验证码已发送到邮箱："+email);

    }

    @Override
    public HealthR<List<String>> findSystemImagePic() {

        //查询用户头像路径
        List<String> path = medicMapper.findSystemImagePic();
        return HealthR.ok(path, "查询成功");
    }

    @Override
    public HealthR<List<DoctorJobTitleVo>> findJobTitleList() {
        List<DoctorJobTitleVo> list = medicMapper.findJobTitleList();
        if (list != null) {
            return HealthR.ok(list, "查询成功");
        }
        return HealthR.fail("查询失败");
    }

    @Override
    public HealthR checkCode(String code, String email) {
        String s = redisTemplate.opsForValue().get(email);
        if (!s.equals(code)) {
            return HealthR.fail("验证码错误");
        }
        return HealthR.ok("验证码正确");
    }

    @Override
    public HealthR resetUserPwd(String email, String pwd1, String pwd2) {
        //判断email是否存在
        String s = medicMapper.checkEmailUnique(email);
        if ( s== null) {
            return HealthR.fail("邮箱不存在");
        }
        if (!pwd1.equals(pwd2)) {
            return HealthR.fail("两次密码不一致");
        }
        if (medicMapper.resetUserPwd(email,pwd1,pwd2) > 0) {
            return HealthR.ok("修改成功");
        }
        return HealthR.fail("操作失败");
    }
}
